package dk.modified.reshary;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

import dk.modified.reshary.GlobalState.TrackerName;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

public class IntroScreen extends Activity {

	AlertDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    Tracker t = ((GlobalState) getApplication()).getTracker(
		        TrackerName.APP_TRACKER);
		    t.setScreenName("Reshary Intro");
		    t.send(new HitBuilders.AppViewBuilder().build());;
		setContentView(R.layout.activity_intro_screen);
		buildAlertDialog();
		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new FragmentPageIntro()).commit();
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub

		dialog.show();

	}

	public void finishIntro() {
		SharedPreferences sharedpreferences = getApplicationContext()
				.getSharedPreferences("dk.modified.reshary",
						Context.MODE_PRIVATE);
		Editor editor = sharedpreferences.edit();
		editor.putBoolean("showintro", false);
		editor.commit();
		Intent j = new Intent(getApplicationContext(), SplashScreen.class);
		j.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_CLEAR_TASK);
		startActivity(j);
	}

	private void buildAlertDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		// Add the buttons
		builder.setPositiveButton(R.string.intro_skip_yes,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finishIntro();
					}
				});
		builder.setNegativeButton(R.string.intro_skip_no,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						// User cancelled the dialog

					}
				});
		// Set other dialog properties
		builder.setTitle(R.string.intro_skip_text);
		builder.setIcon(R.drawable.ic_launcher);
		// Create the AlertDialog
		dialog = builder.create();
	}
}
